using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FixedArray<T> where T : struct
{
    static Stack<T[]>[] pools = System.Array.Empty<Stack<T[]>>();

    static int c2id(int capacity)
    {
        return Mathf.CeilToInt(Mathf.Log(Mathf.Max(capacity, 1), 2.0f));
    }

    public static T[] Get(int capacity)
    {
        if (pools.Length == 0)
            pools = new Stack<T[]>[32];

        int poolId = c2id(capacity);
        if (null != pools[poolId] && pools[poolId].Count > 0)
            return pools[poolId].Pop();

        return new T[1 << poolId];
    }

    public static void Put(T[] array)
    {
        if (null != array)
        {
            int poolId = c2id(array.Length);
            if (poolId < array.Length)
            {
                if (null == pools[poolId])
                    pools[poolId] = new Stack<T[]>(64);
                pools[poolId].Push(array);
            }
        }
    }
}
